<?php

namespace app\common\model\lottery;

use think\Cache;
use app\common\model\BaseModel;
use app\common\model\UploadFile;
/**
 * 抽奖奖品模型
 * Class Prize
 * @package app\common\model\lottery
 */
class Prize extends BaseModel
{
    protected $name = 'lottery_prize';

    /**
     * 图片
     * @return \think\model\relation\HasOne
     */
    public function image()
    {
        return $this->hasOne(UploadFile::class, 'file_id', 'image_id');
    }

     /**
     * 修改状态
     */
    public function setStatus($state)
    {
        return $this->allowField(true)->save(['status' => $state ? 1 : 0]) !== false;
    }

    /**
     * 根据商品id集获列表
     */
    public function getListByIds($prizeIds, $param = [])
    {
        // 默认条件
        $param = array_merge([
            'status' => null,
            'limit' => 15,
        ], $param);

        // 城市 抽奖产品
        $where = [];
        if (isset($param['province']) && $param['province'] > 0) {
            $where = ['province_id' => $param['province'], 'city_id' => $param['city']];
        }
        // 筛选条件
        !is_null($param['status']) && $this->where('status', '=', (int)$param['status']);
        // 获取商品列表数据
        return $this->alias('lottery_prize')
            ->with('image')
            ->where('lottery_prize.prize_id', 'in', $prizeIds)
            ->where('lottery_prize.is_delete', '=', 0)
            ->where($where)
            ->order(['lottery_prize.sort' => 'asc'])
            ->paginate($param['limit'], false, [
                'query' => \request()->request()
            ]);
    }


}